package org.ssp.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ssp.dao.UserDao;
import org.ssp.impl.UserImpl;
import org.ssp.pojo.User;
import org.ssp.util.CommonUtil;

import net.sf.json.JSONObject;

/**
 * 
 * 用微信账号进行登录
 * 
 * @author susp
 *
 */

@WebServlet("/sign")
public class SignServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method=request.getParameter("method");
		String code=request.getParameter("code");	
		String state=request.getParameter("state");//判断是微信客户端授权登录还是网页授权登录
		System.out.println("method---"+method+",code====="+code+",state====="+state);
		String openid=null;
		String access_token=null;
		if(code!=null){
			//通过code换取access_token
			String urlApi="https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxf34da76143f5050d&secret=7a6df503a9afaa70601d652cfb99b16d&code=CODE&grant_type=authorization_code";
			String url=urlApi.replace("CODE", code);
			try {
				String str=CommonUtil.httpsRequest(url, "POST", null);
				System.out.println("------------"+str);
	            //通过json-lib的API把json字符串转化成java对象
				JSONObject jsonObject=JSONObject.fromObject(str);
				if(null!=jsonObject){
					access_token=jsonObject.getString("access_token");
					openid=jsonObject.getString("openid");
					System.out.println("access_token____:"+access_token+",opeind______"+openid);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		System.out.println("---access_token----"+access_token);
		if(method!=null){
			if("menuclick".equals(method)){
				UserDao userdao=new UserImpl();
				if(userdao.findById(openid)==null){
//					System.out.println("code="+code+",state="+state+",access-token="+access_token);
//					request.getRequestDispatcher("/login.jsp?code="+code+"&state="+state).forward(request, response);
					response.sendRedirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf34da76143f5050d&redirect_uri=http%3a%2f%2f15056715.ngrok.natapp.cn%2foec%2flogin.jsp&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect");
					
				}else{
					request.getRequestDispatcher("/page1.jsp").forward(request, response);
				}
	
			}else if("urlclick".equals(method)){//关注成功后进行绑定操作
				String password=request.getParameter("password");
				UserDao userdao=new UserImpl();
				//通过acccess_token和OpenID获取到微信用户个人信息
				String urlapi2="https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
				System.out.println("access_token.........."+access_token);
				String url2=urlapi2.replace("ACCESS_TOKEN", access_token).replace("OPENID", openid);
				try {
					String str2=CommonUtil.httpsRequest(url2, "GET", null);
					System.out.println(str2);
				    //通过json-lib的API把json字符串转化成java对象
					JSONObject jsonObject=JSONObject.fromObject(str2);	
					if(null!=jsonObject){
						System.out.println("用户信息"+str2);
					}
			
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				User user=new User();
				user.setOpenid(openid);
				user.setPassword(password);
				userdao.addUser(user);
			    request.getRequestDispatcher("/page1.jsp").forward(request, response);
			}
		}
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
